/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageData;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.form.LoginForm;
import io.renren.modules.app.service.UserService;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.carinfo.vo.CarInfoVO;
import io.renren.modules.sys.form.PasswordForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestBody LoginForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        //用户登录
        long userId = userService.login(form);

        //生成token
        String token = jwtUtils.generateToken(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    }

    /**
     * 修改登录用户密码
     */
    @Login
    @PostMapping("/changepassword")
    public R password(@RequestBody PasswordForm form, HttpServletRequest request){
        Long userId = (Long) request.getAttribute("userId");
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = DigestUtils.sha256Hex(form.getPassword());
        //sha256加密
        String newPassword = DigestUtils.sha256Hex(form.getNewPassword());

        //更新密码
        boolean flag = userService.updatePassword(userId, password, newPassword);
        if(!flag){
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息表(分页)
     */
    @ApiOperation("用户信息表(分页)")
    @GetMapping("/userinfolist")
    public R userinfolist(@RequestParam Map<String, Object> params){
        PageData<UserEntity> pageData = userService.userinfolist(params);
        return R.ok().put("page", pageData);
    }
}
